//
//  HYGameViewController.m
//  HaiYue
//
//  Created by skyline on 2017/12/14.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYGameViewController.h"
#import "HYGameTableView.h"
#import "HYInfoFloorTableView.h"
#import "HYGameInfoViewController.h"
@interface HYGameViewController ()<UIScrollViewDelegate,HYGameTableViewDelegate,HYInfoFloorTableViewDelegate>
@property (nonatomic, strong) HYGameTableView *onlineV;
@property (nonatomic, strong) HYGameTableView *htmlV;
@property (nonatomic, strong) HYGameTableView *sigleV;
@property (nonatomic, strong) HYInfoFloorTableView *infoV;
@property (nonatomic, copy) NSString *api;
@property (nonatomic, assign) NSInteger httpIndex;
@end

@implementation HYGameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"游戏中心";
    //    self.segNaviBar.items = @[@"网游",@"H5",@"单机",@"资讯"];
    
    self.infoV.hidden = NO;
    _httpIndex = 0;
    [self request];
    
    
    // Do any additional setup after loading the view.
}

- (HYGameTableView *)onlineV
{
    if (!_onlineV)
    {
        _onlineV = [[HYGameTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _onlineV.type = HYGameTableViewTypeCycleHeader;
        _onlineV.hydelegate = self;
        _onlineV.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForGameVCLoadNew:)];
        //        _onlineV.mj_footer = [HYRefreshFooder  footerWithRefreshingTarget:self refreshingAction:@selector(actionForGameVCLoadMore:)];
        _onlineV.tableFooterView = [[UIView alloc] init];
        [self.scrollView addSubview:_onlineV];
        [_onlineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView);
            make.left.equalTo(self.scrollView);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT));
        }];
    }
    return _onlineV;
}

- (HYGameTableView *)htmlV
{
    if (!_htmlV)
    {
        _htmlV = [[HYGameTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _htmlV.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForGameVCLoadNew:)];
        _htmlV.tableFooterView = [[UIView alloc] init];
        _htmlV.hydelegate = self;
        [self.scrollView addSubview:_htmlV];
        [_htmlV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView);
            make.left.equalTo(self.scrollView).with.offset(SCREEN_WIDTH);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT));
        }];
    }
    return _htmlV;
}

- (HYGameTableView *)sigleV
{
    if (!_sigleV)
    {
        _sigleV = [[HYGameTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _sigleV.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForGameVCLoadNew:)];
        _sigleV.tableFooterView = [[UIView alloc] init];
        _sigleV.hydelegate = self;
        [self.scrollView addSubview:_sigleV];
        [_sigleV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView);
            make.left.equalTo(self.scrollView).with.offset(SCREEN_WIDTH * 2);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT));
        }];
    }
    return _sigleV;
}

- (HYInfoFloorTableView *)infoV
{
    if (!_infoV)
    {
        _infoV = [[HYInfoFloorTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _infoV.mj_header = [HYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(actionForGameVCLoadNew:)];
        _infoV.tableFooterView = [[UIView alloc] init];
        _infoV.hydelegate = self;
        [self.scrollView addSubview:_infoV];
        [_infoV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView);
            make.left.equalTo(self.scrollView).with.offset(SCREEN_WIDTH * 3);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT));
        }];
    }
    return _infoV;
}

- (void)setHttpIndex:(NSInteger)httpIndex
{
    if (_httpIndex != httpIndex)
    {
        _httpIndex = httpIndex;
    }
    [self https];
}

- (void)actionForGameVCLoadNew:(UITableView *)view
{
    self.httpIndex = self.segNaviBar.currentIndex;
}

- (void)actionForGameVCLoadMore:(UITableView *)view
{
    
}
- (void)actionForRightNaviBar
{
    
}
- (void)actionForOnlineMidCellMore
{
    DeLog(@"进入预约游戏页面");
}

- (void)actionForOnlineTopSelectIMG:(NSNotification *)notify
{
    HYGameInfoViewController *infoVC = [[HYGameInfoViewController alloc]init];
    infoVC.gameid = [notify.userInfo valueForKey:@"gameid"];
    [self.navigationController pushViewController:infoVC animated:YES];
}
- (void)actionFoneOnlineCell:(NSNotification *)notify
{
    HYGameInfoViewController *infoVC = [[HYGameInfoViewController alloc]init];
    infoVC.gameid = [notify.userInfo valueForKey:@"gameid"];
    [self.navigationController pushViewController:infoVC animated:YES];
}

- (void)request
{
    for (int i = 0; i < self.segNaviBar.items.count; i++)
    {
        self.httpIndex = i;
    }
}

- (void)https
{
    if (self.httpIndex == 0) self.api = API_GAME_ONLINE;
    else if (self.httpIndex == 1) self.api = API_GAME_H5;
    else if (self.httpIndex == 2) self.api = API_GAME_SINGLE;
    else if (self.httpIndex == 3) self.api = API_GAME_NOTICE;
    
    [self startHUD];
    
    __block NSURLSessionTask *task  = [CCHttp post:self.api para:nil success:^(id  _Nonnull response) {
        [self endHUD];
        //        [CCSave archiver:self.model toFile:ONLINEGAME_MODEL];
        if ([task.currentRequest.URL.absoluteString containsString:API_GAME_ONLINE])
        {
            self.onlineV.model = [HYGameListModel mj_objectWithKeyValues:response];
            [self.onlineV reloadData];
            [self.onlineV.mj_header endRefreshing];
            [self.onlineV.mj_footer endRefreshing];
        }
        else if ([task.currentRequest.URL.absoluteString containsString:API_GAME_H5])
        {
            self.htmlV.model = [HYGameListModel mj_objectWithKeyValues:response];
            [self.htmlV reloadData];
            [self.htmlV.mj_header endRefreshing];
            [self.htmlV.mj_footer endRefreshing];
        }
        else if ([task.currentRequest.URL.absoluteString containsString:API_GAME_SINGLE])
        {
            self.sigleV.model = [HYGameListModel mj_objectWithKeyValues:response];
            [self.sigleV reloadData];
            [self.sigleV.mj_header endRefreshing];
            [self.sigleV.mj_footer endRefreshing];
        }
        else if ([task.currentRequest.URL.absoluteString containsString:API_GAME_NOTICE])
        {
            self.infoV.model = [HYGameListModel mj_objectWithKeyValues:response];
            [self.infoV reloadData];
            [self.infoV.mj_header endRefreshing];
            [self.infoV.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
    }];
    
    
    //    [HYHttps post:self.api para:nil completeWithTask:^(NSURLSessionDataTask *task, id response) {
    //        [self endHUD];
    //        //        [CCSave archiver:self.model toFile:ONLINEGAME_MODEL];
    //        if ([task.currentRequest.URL.absoluteString containsString:API_GAME_ONLINE])
    //        {
    //            self.onlineV.model = [HYGameListModel mj_objectWithKeyValues:response];
    //            [self.onlineV reloadData];
    //            [self.onlineV.mj_header endRefreshing];
    //            [self.onlineV.mj_footer endRefreshing];
    //        }
    //        else if ([task.currentRequest.URL.absoluteString containsString:API_GAME_H5])
    //        {
    //            self.htmlV.model = [HYGameListModel mj_objectWithKeyValues:response];
    //            [self.htmlV reloadData];
    //            [self.htmlV.mj_header endRefreshing];
    //            [self.htmlV.mj_footer endRefreshing];
    //        }
    //        else if ([task.currentRequest.URL.absoluteString containsString:API_GAME_SINGLE])
    //        {
    //            self.sigleV.model = [HYGameListModel mj_objectWithKeyValues:response];
    //            [self.sigleV reloadData];
    //            [self.sigleV.mj_header endRefreshing];
    //            [self.sigleV.mj_footer endRefreshing];
    //        }
    //        else if ([task.currentRequest.URL.absoluteString containsString:API_GAME_NOTICE])
    //        {
    //            self.infoV.model = [HYGameListModel mj_objectWithKeyValues:response];
    //            [self.infoV reloadData];
    //            [self.infoV.mj_header endRefreshing];
    //            [self.infoV.mj_footer endRefreshing];
    //        }
    //
    //    } fail:^(NSDictionary *dict) {
    //        [self endHUD];
    //    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    offset.y = (offset.y >= 0)?0:offset.y;
    scrollView.contentOffset = offset;
}

- (void)regitsterNotice
{
    [CCNotice addObs:self sel:@selector(actionForOnlineMidCellMore) name:CCNoticeNameOnlineMidCellMore obj:nil];
    [CCNotice addObs:self sel:@selector(actionForOnlineTopSelectIMG:) name:CCNoticeNameCycleScrollClick obj:nil];
    [CCNotice addObs:self sel:@selector(actionFoneOnlineCell:) name:CCNoticeNameOnlineCellClick obj:nil];
}
- (void)removeNotice
{
    [CCNotice rmObs:self name:CCNoticeNameOnlineMidCellMore obj:nil];
    [CCNotice rmObs:self name:CCNoticeNameCycleScrollClick obj:nil];
    [CCNotice rmObs:self name:CCNoticeNameOnlineCellClick obj:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self regitsterNotice];
    self.style = (IS_APPStore)?HYViewNaviBarStyleSeg:HYViewNaviBarStyleOnlyRightBtn;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self removeNotice];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
